<template>
	<!-- 分类 -->
	<view class="public">
		<view class="search" @click="tosousuo">
			<u-search v-model="keyword" :showAction="false" height="30" disabled="" placeholder="请输入搜索关键词"></u-search>
		</view>
		<view class="content_left">
			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y">
				<view id="demo1" class="scroll-view-item uni-bg-red" v-for="(item,index) in list2"
					@click="childdetail(index,item.id)">
					<view class="scroll_child">
						<view :class="activechild == index?'activeClass':'ptchild'">{{item.name}}
							<view class="yesthing" v-if="item.badge.value!=0">{{item.badge.value}}</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="content_right">
			<view class="content_right_top">
				<view :class="is_jx==1?'itemscreenfocus':'itemscreen'" @click="cuxiao">精选</view>
				<view :class="is_new==1?'itemscreenfocus':'itemscreen'" @click="xinpin">新品</view>
				<view :class="is_hot==1?'itemscreenfocus':'itemscreen'" @click="hot">热销</view>
				
				<!-- <view class="top_little" :class="is_jx==1?'bdhuang':''" @click="cuxiao">
					<text style="color: #F5AC4F;">精选</text>
					<image src="../../static/smallIcon/cuxiao.png" mode=""></image>
				</view>
				<view class="top_little" :class="is_jx==1?'bdhuang':''" @click="cuxiao">
					<text style="color: #F5AC4F;">新品</text>
					<image src="../../static/smallIcon/cuxiao.png" mode=""></image>
				</view>
				<view class="top_little" :class="is_hot==1?'bdhong':''" @click="hot">
					<text style="color: #FF0000;">热销</text>
					<image src="../../static/smallIcon/rexiao.png" mode=""></image>
				</view> -->
			</view>
			<view class="flexcenter" style="margin-top: 300rpx;" v-if="!list.data.length">
				<view class="placeholderimage"></view>
			</view>
			<view class="content_center">
				<scroll-view :scroll-top="scrollTop" scroll-y="true" @scrolltolower="onReachBottomt"
					refresher-threshold="100" class="scroll-Ys">
					<view class="center_child" v-for="(item,index) in list.data" :key="item.id"
						@click="getCommodityDetail(item.id)">
						<view class="child_left">
							<image :src="item.image_text" mode=""></image>
						</view>
						<view class="child_right" style="height: 200rpx ;">
							<view class="right_title">{{item.name}}</view>
							<view class="right_little">
								<view class="little_song" v-if="item.activity_title!=null && item.activity_title!=''">
									{{item.activity_title}}
								</view>
								<view class="xiaohuang">积分：{{item.smallgoodsunit.priceinfo.buy_send_score}}分</view>
							</view>
							<view class="right_little flexbetween">
								<!-- <view class="little_jifen">库存:{{item.goods_num}}</view> -->
								<view class="little_jifen">月销:{{item.mouth_sales}}</view>
							</view>
							<view class="right_bottom">
								<view class="money">￥{{item.smallgoodsunit.priceinfo.prev_xs_price?item.smallgoodsunit.priceinfo.prev_xs_price:item.smallgoodsunit.priceinfo.price || 0}}</view>
								<view class="" @click.stop="addCart(item)">
									<uni-icons type="cart" size="24" color="rgb(245,172,79)"></uni-icons>
								</view>
							</view>
						
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="posixidan">
			<view class="lefthui flexcenter">
				<view class="flexcolumn">
					<view class="" style="font-size: 32rpx;">￥{{allprice}}</view>
					<view class="" style="font-size: 28rpx;">{{num}}种，{{allnum}}件</view>
				</view>
				<view class="righthuang flexcenter" @click="goToShopping">下单结算</view>
			</view>
		</view>
		<tabbar current=1 ref="cars"></tabbar>
	</view>
</template>

<script>
	import httpRequest from '@/commonFun/httpRequest.js'
	export default {
		data() {
			return {
				show: false,
				keyword: "",
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				activechild: 0,
				fenleiId: '',
				list2: [{
					count: 0,
					id: "",
					name: "全部"
				}],
				page: 1,
				limit: 10,
				list: {
					data: [],
				},
				is_hot: 0,
				is_jx: 0,
				is_new:0,
				is_login:false,
				num: 0,
				allnum: 0,
				allprice: 0,
				allscore: 0,
				carlist:[]
			};
		},
		onLoad() {
			let _this = this;
			uni.hideTabBar(); //隐藏tab
			_this.fenlei();
		},
		onShow() {
			this.is_login = httpRequest.checkIsLogin();
			if(this.is_login){
				this.getCartData(); //获取购物车数据
			}
			let _this = this;
			if (httpRequest.checkIsLogin()) {
				this.$refs.cars.getlist()
			}

		},
		methods: {
			getCartData() {
				let _this = this;
				httpRequest.request('/customapi/cart/cartList', 'GET', {}).then(res => {
					if (res.code == 1) {
						let num = 0
						let allnum = 0
						let allprice = 0
						let allscore = 0
						res.data.forEach(item => {
							num = num + 1
							allnum = Number(allnum) + Number(item.number)
							allprice = Number(allprice) + item.number * (Number(item.sku.prev_xs_price)?Number(item.sku.prev_xs_price):Number(item.sku.price))
							allscore = Number(allscore) + item.number * item.sku.buy_send_score
						})
						this.num = num
						this.allnum = allnum
						this.allprice = allprice
						this.allscore = allscore
						
						this.carlist = res.data
					} else {
						httpRequest.toast(res.msg);
					}
				})
			},
			goToShopping() {
				if (!httpRequest.checkIsLogin()) {
					uni.navigateTo({
						url: '/pages/login/logintwice'
					})
					return;
				}
				let _this = this;
				let data = {
					ly_type:"cart",//来源，detail详情，cart购物车
					order_type: 1,
					data: []
				}
				this.carlist.forEach(item => {
					data.data.push({
						goods_id: item.goods_id,
						num: item.number,
						goods_sku_id: item.sku_id,
						goods_type: 1,
						user_coupon_id: "",
						price: item.sku.prev_xs_price?item.sku.prev_xs_price:item.sku.price,
					})
				})
				if (data.data.length == 0) {
					httpRequest.toast('请添加商品到购物车')
					return false
				}
				uni.navigateTo({
					url: "/pages/shoppingCart/orderDetail?data=" + encodeURIComponent(JSON.stringify(data))
				})
			},
			//添加购物车
			addCart(item) {
				let _this = this;
				if (!httpRequest.checkIsLogin()) {
					uni.navigateTo({
						url: '/pages/login/logintwice'
					})
					return;
				}
				httpRequest.request('/customapi/cart/addCart', 'GET', {
					goods_id: item.id,
					number: 1,
					sku_id: item.smallgoodsunit.priceinfo.id
				}).then(res => {
					httpRequest.toast(res.msg)
					if(res.code==1){
						this.$refs.cars.getlist()
						this.getCartData()
					}
				})
			},
			getlist(isPage, page) {
				let _this = this;
				httpRequest.request('/customapi/goods/index', 'POST', {
					category_id: _this.fenleiId,
					page: page || 1,
					limit: _this.limit,
					keyword: this.keyword,
					is_scoredh: 0,
					is_hot: this.is_hot,
					is_jx: this.is_jx,
					is_new:this.is_new,
				}).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data.push(...resList.data);
						} else {
							_this.list = res.data;
							_this.list.data = resList.data;
						}
					} else {
						httpRequest.toast(res.msg);
					}
				})
			},
			onReachBottomt() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.lastpage <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.getlist(true, page);
			},
			fenlei() {
				let _this = this;
				httpRequest.request('/customapi/goods/goodsCategory', 'GET', {}).then(res => {
					if (res.code == 1) {
						let arr = []
						res.data.forEach(item => {
							arr.push({
								badge: {
									value: item.goods_num1,
								},
								id: item.id,
								name: item.name
							})
						})
						arr.unshift({
							badge: {
								value: 0,
							},
							id: "",
							name: "全部"
						})
						this.list2 = arr
						_this.getlist(); //商品列表
					} else {
						httpRequest.toast(res.msg)
					}
				})
			},
			cuxiao() {
				this.is_jx = this.is_jx == 1 ? 0 : 1;
				this.getlist();
			},
			hot() {
				this.is_hot = this.is_hot == 1 ? 0 : 1;
				this.getlist();
			},
			xinpin() {
				this.is_new = this.is_new == 1 ? 0 : 1;
				this.getlist();
			},
			enter() {
				console.log(this.value)
			},
			tosousuo() {
				uni.navigateTo({
					url: '/pages/index/sousuo'
				})
			},
			childdetail(i, id) {
				this.activechild = i;
				this.fenleiId = id;
				this.page = 1
				this.list = {
						list: [],
					},
					this.getlist();
			},
			clickItem(item) {
				uni.navigateTo({
					url: '/pages/index/commodityDetail/commodityDetail?id=' + item.id,
				});
			},
			onClick(i) {
				console.log(i);
				this.hover = i
				console.log(this.hover)
			},
			getCommodityDetail(i) {
				uni.navigateTo({
					url: '/pages/index/commodityDetail/commodityDetail?id=' + i,
				});
			},
		}
	};
</script>

<style scoped lang="scss">
	.itemscreen {
		width: 120rpx;
		height: 65rpx;
		line-height: 65rpx;
		text-align: center;
		border-radius: 10rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #333;
		background-color: #f5f5f5;
		box-sizing: border-box;
		border: 1rpx solid #999;
	}

	.itemscreenfocus {
		width: 120rpx;
		height: 65rpx;
		line-height: 65rpx;
		text-align: center;
		border-radius: 10rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #f5ac4f;
		background-color: rgba(245, 172, 79, .01);
		box-sizing: border-box;
		border: 1rpx solid #f5ac4f;
	}

	.bdhuang {
		border: 2rpx solid #F5AC4F;
	}

	.bdhong {
		border: 2rpx solid #FF0000;
	}

	.inputk {
		border: 1rpx solid #999;
		padding: 20rpx 0;
		border-radius: 10rpx;
		text-align: center;
	}

	.xhtext2 {
		font-size: 24rpx;
		width: 50rpx;
		text-align: center;
	}

	.jianhao2 {
		width: 32rpx;
		height: 20rpx;
		position: relative;
		top: 10rpx;

		.line2 {
			height: 3.5rpx;
			width: 22rpx;
			background-color: #999999;
		}
	}

	.jiajian2 {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 140rpx;
	}

	.posixidan {
		position: fixed;
		bottom: 170rpx;
		right: 30rpx;
	}

	.lefthui {
		height: 100rpx;
		padding: 0 180rpx 0 30rpx;
		background-color: #999999;
		border-radius: 50rpx;
		color: #fff;
		position: relative;
	}

	.righthuang {
		padding: 0 20rpx;
		position: absolute;
		right: -1rpx;
		top: 0;
		border-radius: 50rpx;
		height: 100rpx;
		background-color: #f5ac4f;
		font-size: 32rpx;
	}

	.mainpadding {
		padding: 0 30rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #FFFFFF;

		.addcar {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 80rpx;
			background-color: #F5AC4F;
			border-radius: 50rpx;

		}

		.addcar1 {
			width: 100%;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #fff;
			border: 4rpx solid #F5AC4F;
			color: #F5AC4F;
			border-radius: 50rpx;
		}

		.addcar2 {
			width: 100%;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #999999;
			border: 4rpx solid #999999;
			color: #fff;
			border-radius: 50rpx;
		}

		.buybox {
			width: 45%;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border: 2rpx solid #F5AC4F;
			background-color: #F5AC4F;
			border-radius: 50rpx;
		}
	}

	.strongtext {
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #333333;
		margin-right: 20rpx;
	}

	.shopShowBox {
		padding: 60rpx 20rpx;

		.bottom {
			display: flex;
			justify-content: space-between;

			.bottomLeft {
				display: flex;
				align-items: flex-end;

				.strongtext {
					font-size: 36rpx;
					font-family: PingFang SC;
					font-weight: 800;
					color: #333333;
					margin-right: 20rpx;
				}

				.b2 {
					color: #999999;
					font-size: 28rpx;
				}
			}
		}

		.center {
			.detail {
				font-size: 28rpx;
				display: flex;
				padding: 20rpx 50rpx;
				justify-content: space-between;
				flex-wrap: wrap;

				.item {
					border: 1px solid #999999;
					border-radius: 20px;
					padding: 16rpx 20rpx;
					margin-bottom: 30rpx;
					width: 40%;
					text-align: center;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.dtcss {
					border: 1px solid #F5AC4F;
					color: #F5AC4F;
				}

				.nosel {
					border: 1rpx solid lightgray;
				}
			}
		}

		.top {
			display: flex;
			margin-bottom: 20rpx;

			.topLeft {
				overflow: hidden;
				border-radius: 20rpx;

				image {
					border-radius: 20rpx;
					width: 100px;
					height: 100px;
					margin-right: 20rpx;
				}
			}

			.topRight {
				font-size: 32rpx;
				padding: 30rpx 0;
				display: flex;
				justify-content: space-between;
				flex-direction: column;

				.topRightTop {
					color: #ff0000;
				}

				.topRightBottom {
					display: flex;
				}
			}
		}
	}

	.box {
		padding: 10px 10px 35px 10px;

		.downOrderBox {
			padding: 2rpx 0 2rpx 20rpx;
			background-color: #999999;
			border-radius: 20px;
			position: fixed;
			bottom: 0;
			right: 0;
			display: flex;
			font-size: 0.8rem;
			color: #ffffff;

			.right {
				background-color: #DD9B48;
				padding: 4rpx 36rpx;
				border-radius: 40rpx;
				// line-height: 40px;
				align-items: center;
				display: flex;
			}

			.left {
				.l1 {
					font-size: 0.6rem;
				}
			}
		}

		.bodys {
			width: 450rpx;
			background-color: rgb(246, 246, 246);
			border-radius: 16rpx;
			display: flex;
			margin-top: 40rpx;
			overflow: hidden;
			margin-bottom: 20rpx;

			view:last-child {
				// margin-bottom: 40rpx;
			}

			.left {
				position: relative;
				color: #ffffff;
				font-size: 0.6rem;

				.l1 {
					position: absolute;
					top: 0;
					background-color: #FF0000;
					// border-radius: 8px;
					border-bottom-right-radius: 32rpx;
					padding: 4rpx 14rpx;
					color: #ffffff;
				}

				.l2 {
					position: absolute;
					bottom: 10rpx;
					right: 20rpx;
				}

				image {
					width: 200rpx;
					height: 180rpx;
				}
			}

			.right {
				padding: 20rpx;
				font-size: 0.6rem;
				flex-direction: column;
				display: flex;
				justify-content: space-between;

				.rightBottom {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.RB1 {
						color: #FF0000;
						font-size: 0.7rem;
					}

					image {
						width: 30rpx;
						height: 30rpx;
					}
				}

				.rightTop {
					.jifen {
						display: flex;
						align-items: center;

						.j1 {
							margin-right: 10rpx;
							color: #ffffff;
							background-color: #F5AC4F;
							border-radius: 8rpx;
							padding: 2rpx;
						}
					}

					.title {
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						height: 60rpx;
					}
				}
			}

			.black {
				width: 450rpx;
				position: absolute;
				background-color: rgba(51, 51, 51, 0.5);
				padding: 20rpx 0;
				border-radius: 16rpx;

				.black_text {
					color: #ffffff;
					font-size: 28rpx;
					text-align: center;
					padding: 20rpx 0;
				}

				.black_btn {
					color: #333333;
					background-color: #FFFFFF;
					font-size: 28rpx;
					padding: 18rpx 0;
					border-radius: 30rpx;
					width: 160rpx;
					text-align: center;
					margin: 0 auto;
				}
			}
		}

		.header {
			display: flex;
			justify-content: space-around;

			.header1 {
				background-color: rgb(246, 246, 246);
				padding: 8rpx 40rpx;
				border-radius: 40rpx;
				color: #F5AC4F;
				align-items: center;

				image {
					width: 25rpx;
					height: 25rpx;
					margin-left: 10rpx;
				}
			}

			.header2 {
				background-color: rgb(246, 246, 246);
				padding: 8rpx 40rpx;
				border-radius: 40rpx;
				color: #FF0000;
				align-items: center;

				image {
					width: 25rpx;
					height: 25rpx;
					margin-left: 10rpx;
				}
			}
		}
	}

	.public {
		overflow: hidden;

		.search {
			background-color: #fff;
			padding: 30rpx;
		}
	}

	.yesthing {
		position: absolute;
		top: -25rpx;
		right: -20rpx;
		background-color: #F5AC4F;
		color: #FFFFFF;
		margin-left: 6rpx;
		font-size: 22rpx;
		padding: 2rpx 8rpx;
		border-radius: 50rpx;
	}

	.content_left {
		width: 180rpx;
		background-color: #fff;
		float: left;

		.scroll-view-item {
			padding: 30rpx 10rpx;
			text-align: center;
			color: #999999;
		}

		.scroll_child {
			display: flex;
			flex-wrap: nowrap;
			justify-content: center;
			align-items: flex-start;

			.ptchild {
				color: #999999;
				position: relative;
			}

			.activeClass {
				position: relative;
				color: #333333;
				font-weight: bold;
				border-bottom: 8rpx solid #2C5FAE;
			}

			.scroll_little {
				font-size: 22rpx;
				width: 20rpx;
				background-color: #F5AC4F;
				margin-top: -5rpx;
			}
		}

	}

	.little_song2 {
		padding: 5rpx 15rpx;
		background-color: #F5AC4F;
		// width: 80rpx;
		font-size: 22rpx;
		border-radius: 30rpx;
		color: #FFFFFF;
	}

	.content_right {
		float: left;
		width: calc(100vw - 180rpx);

		.content_right_top {
			// margin-top: 10rpx;
			display: flex;
			flex-wrap: nowrap;
			justify-content: space-around;
			padding:15rpx 30rpx;

			// position: sticky;
			// top: 10rpx;
			.top_little {
				background-color: #FFFFFF;
				border-radius: 40rpx;
				font-size: 30rpx;
				padding: 20rpx 40rpx;
				box-sizing: border-box;

				image {
					width: 30rpx;
					height: 30rpx;
					margin-left: 10rpx;
					margin-bottom: -5rpx;
				}
			}
		}

		.content_center {
			width: calc(100vw - 200rpx);
			margin: 0 auto;

			.center_child {
				position: relative;
				margin: 20rpx 0 0;
				background-color: #fff;
				border-radius: 25rpx 25rpx 25rpx 25rpx;
				flex-wrap: nowrap;
				display: flex;
				justify-content: flex-start;
				position: relative;

				.child_left {
					width: 200rpx;
					height: 200rpx;
					position: relative;

					image {
						width: 200rpx;
						height: 200rpx;
						border-top-left-radius: 25rpx;
						border-bottom-left-radius: 25rpx;
					}

					.left_little {
						position: absolute;
						top: 0;
						left: 0;
						background-color: #FF0000;
						border-top-left-radius: 24rpx;
						border-bottom-right-radius: 24rpx;
						padding: 5rpx 10rpx;
						font-size: 28rpx;
						color: #FFFFFF;
						max-width: 160rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;

					}
				}

				.child_right {
					padding: 10rpx 20rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					width: 100%;
					box-sizing: border-box;

					.right_title {
						font-size: 32rpx;
						font-weight: bold;
						color: #333;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 1;
					}

					.right_little {
						display: flex;
						flex-wrap: nowrap;
						justify-content: space-between;

						.little_song {
							background-color: #F5AC4F;
							width: 80rpx;
							font-size: 22rpx;
							border-radius: 10rpx;
							text-align: center;
							color: #FFFFFF;
						}

						.little_jifen {
							font-size: 28rpx;
						}
					}

					.right_bottom {
						display: flex;
						flex-wrap: nowrap;
						justify-content: space-between;

						.money {
							color: #FF0000;
							font-size: 30rpx;
						}

						.right_icon {
							width: 30rpx;

							image {
								width: 100%;
							}
						}
					}

				}

				.child_mask {
					width: 480rpx;
					height: 210rpx;
					position: absolute;
					top: 0;
					left: 0;
					background-color: rgba(51, 51, 51, 0.5);
					border-radius: 30rpx;

					.buhuo {
						text-align: center;
						color: #FFFFFF;
						padding: 40rpx 0 20rpx;
						font-size: 36rpx;
						font-weight: bold;
					}

					.service {
						width: 120rpx;
						margin: 0 auto;
						background-color: #FFFFFF;
						padding: 20rpx 40rpx;
						border-radius: 40rpx;
						font-size: 30rpx;
						color: #333333;
					}
				}
			}
		}

	}

	.scroll-Y {
		height: calc(100vh - 54px - 150rpx);
	}

	.scroll-Ys {
		// height: calc(100vh - 100px - 300rpx);
		height: calc(100vh - 500rpx);
	}

	.scroll-view-item {
		height: 50rpx;
		font-size: 28rpx;
	}
</style>